//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006-2007
//
// Nodes of routing graph.
//

# include "StdAfx.h"
# include "NodesExtra.h"
# include "RGNodes.h"

bool numbers_t::operator == (const numbers_t & _other) const {
	if (::memcmp (this, & _other, sizeof (* this)) == 0)
		return true;
	if (! bHasNumbers && ! _other.bHasNumbers)
		return true;
	return false;
}

void numbers_t::ReverseSides (numbers_t * _pPrev, bool _bLast) {
	const WORD wLeftAfter = this->wLeftAfter;
	this->wLeftAfter   = this->wRightBefore;
	this->wRightBefore = wLeftAfter;

	const WORD wRightAfter = this->wRightAfter;
	this->wRightAfter = this->wLeftBefore;
	this->wLeftBefore = wRightAfter;

	if (_pPrev) {
		_pPrev->wLeftParity     = this->wRightParity;
		_pPrev->wRightParity    = this->wLeftParity;
		_pPrev->pZipLeftAfter   = this->pZipRightAfter;
		_pPrev->pZipRightAfter  = this->pZipLeftAfter;
		_pPrev->pCityLeftAfter  = this->pCityRightAfter;
		_pPrev->pCityRightAfter = this->pCityLeftAfter;
	}

	if (_bLast) {
		this->wRightParity    = 0;
		this->wLeftParity     = 0;
		this->pZipRightAfter  = NULL;
		this->pZipLeftAfter   = NULL;
		this->pCityRightAfter = NULL;
		this->pCityLeftAfter  = NULL;
	}
}

/////////////////////////////////////////////////////////

bool point_extra_t::empty () const {
	return pRGNode == NULL && ! Numbers.bHasNumbers;
}

bool point_extra_t::operator == (const point_extra_t & _other) const {
	return pRGNode == _other.pRGNode && Numbers == _other.Numbers;
}

void point_extra_t::SetRGNode (rgnode_t * _pRGNode, bool _bEndNode) {
	if (pRGNode) {
		-- pRGNode->btPolylines;

		if (bEndNode)
			-- pRGNode->btCardinality;
		else
			pRGNode->btCardinality -= 2;
	}

	pRGNode  = _pRGNode;
	bEndNode = _bEndNode;

	if (pRGNode) {
		++ pRGNode->btPolylines;

		if (bEndNode)
			++ pRGNode->btCardinality;
		else
			pRGNode->btCardinality += 2;
	}
}

/////////////////////////////////////////////////////////

bool IsNotEmpty (const point_extras_t & _PointExtras) {
	const size_t cExtras = _PointExtras.size ();
	for (size_t c = 0; c < cExtras; ++ c)
		if (! _PointExtras [c].empty ())
			return true;

	return false;
}

bool HasRGNodes (const point_extras_t & _PointExtras) {
	const size_t cExtras = _PointExtras.size ();
	for (size_t c = 0; c < cExtras; ++ c)
		if (_PointExtras [c].GetRGNode () != NULL)
			return true;

	return false;
}

bool HasNumbers (const point_extras_t & _PointExtras) {
	const size_t cExtras = _PointExtras.size ();
	for (size_t c = 0; c < cExtras; ++ c)
		if (_PointExtras [c].Numbers.bHasNumbers)
			return true;

	return false;
}
